local Util = require("common.Util")
local CombatBuff = require("combat.CombatBuff")
local CombatDefine = require("combat.CombatDefine")
local CombatImpl = require("combat.CombatImpl")
local Skill = require("combat.Skill")
local CombatObj = require("combat.CombatObj")
local RoleAttr = require("role.RoleAttr")
local RoleDefine = require("role.RoleDefine")
local HeroExcel = require("excel.hero").hero
local SkillExcel = require("excel.skill").skill
local BufferExcel = require("excel.buffer").buffer
local FuwenExcel = require("excel.fuwen")
local SkinSkillExcel = require("excel.skin").skill
local BingshuLogic = require("fuwen.BingshuLogic")
local ProjectLogic = require("platform.ProjectLogic")
local HeroDefine = require("hero.HeroDefine")



local tbFuwenSkillGroupID = {}
local posSortBySpeed = posSortBySpeed or {}

function initBeSkill(obj)
	obj.beSkillList = {}
	obj.beSkillListTemp = {}
end

function setBeSkillID(obj, skillID, isFuWenSkill)
	if obj.beSkillListTemp[skillID] then return end
    local skillConfig = SkillExcel[skillID]
	if skillConfig.need > obj.quality then return end
	if skillConfig.beSkillType == 0 then return end

	if skillConfig.beSkillType == BESKILL_TYPE24 then
        obj.beSkillListTemp[BESKILL_TYPE24] = obj.beSkillListTemp[BESKILL_TYPE24] or {}
		obj.beSkillListTemp[BESKILL_TYPE24][#obj.beSkillListTemp[BESKILL_TYPE24] + 1] =  {
           skillID,
           skillConfig.beSkillLimit > 0 and skillConfig.beSkillLimit,skillConfig.cd[1] or 0,
           skillConfig.cd[2] or 0, 
           isFuWenSkill or 0}

		for _,skillID in ipairs(skillConfig.args) do
			local skillConfig = SkillExcel[skillID]
			if not obj.beSkillListTemp[skillID] and skillConfig.need <= obj.quality then
				obj.beSkillList[skillConfig.beSkillType] = obj.beSkillList[skillConfig.beSkillType] or {}
				obj.beSkillList[skillConfig.beSkillType][#obj.beSkillList[skillConfig.beSkillType] + 1] = {
                skillID,
                skillConfig.beSkillLimit > 0 and skillConfig.beSkillLimit,skillConfig.cd[1] or 0,
                skillConfig.cd[2] or 0, 
                isFuWenSkill or 0}

				obj.beSkillListTemp[skillID] = 1
			end
		end
	else
		obj.beSkillList[skillConfig.beSkillType] = obj.beSkillList[skillConfig.beSkillType] or {}
		obj.beSkillList[skillConfig.beSkillType][#obj.beSkillList[skillConfig.beSkillType] + 1] = {
        skillID,
        skillConfig.beSkillLimit > 0 and skillConfig.beSkillLimit,skillConfig.cd[1] or 0,
        skillConfig.cd[2] or 0, 
        isFuWenSkill or 0}
		obj.beSkillListTemp[skillID] = 1
	end
end

function setBeSkill(obj, config, skinConf, monsterOutConfig)
	local temp = {}
	local confBeskillID = skinConf and skinConf.beSkillList or config.beSkillList
	for _,skillID in ipairs(confBeskillID) do
		setBeSkillID(obj,skillID)
	end

    -- 加载 赋能技能 这个策划说放到最后
	if config.bePvPSkillList and CombatImpl.isPVP == 1 then
	    for _,skillID in ipairs(config.bePvPSkillList) do
	        setBeSkillID(obj, skillID)
	    end
	end

    -- 怪物组附加的被动技能
    if monsterOutConfig then
       for _, skillID in ipairs(monsterOutConfig) do
       	  setBeSkillID(obj,skillID)
       end
    end
	
	-- 符文被动技能
	-- 符文技能效果这里 如果有多个同类型的技能 效果不叠加 只取最高等级的那个技能的效果
	if obj.fuwen or obj.bingshu then
		local hasFuwenBeSkill = nil
		Util.initTable(tbFuwenSkillGroupID)
		if obj.fuwen then
			for i = 1, 2 do
				local fuwenGrid = obj.fuwen[i]
				if fuwenGrid and fuwenGrid.skill then
	                for j = 1, #fuwenGrid.skill do
						local fuwenSkillID = fuwenGrid.skill[j]
	                    local fuwenSkillConfig = FuwenExcel.skill[fuwenSkillID]
	                    local fuwenGroupID = fuwenSkillConfig.groupID
						if fuwenSkillConfig.fuwenBeSkill ~= 0 then
							hasFuwenBeSkill = true
							if tbFuwenSkillGroupID[fuwenGroupID] then
								if tbFuwenSkillGroupID[fuwenGroupID] < fuwenSkillID then
									tbFuwenSkillGroupID[fuwenGroupID] = fuwenSkillID
								end
							else
								tbFuwenSkillGroupID[fuwenGroupID] = fuwenSkillID
							end
						end
	                end
				end
			end
		end
		if obj.bingshu then
	        for i = 1, BingshuLogic.BINGSHU_MAXCNT do
	            local bingshuGrid = obj.bingshu[i]
	            if bingshuGrid then
	                local skillID = bingshuGrid.skillID
	                local skillConfig = FuwenExcel.skill[skillID]
	                local groupID = skillConfig.groupID
	                hasFuwenBeSkill = true
	                if (tbFuwenSkillGroupID[groupID] or 0) < skillID then
	                    tbFuwenSkillGroupID[groupID] = skillID
	                end             
	            end
	        end
		end

		if hasFuwenBeSkill == true then
			for groupID, skillIDTemp in pairs(tbFuwenSkillGroupID) do
				local fuwenSkillConfig = FuwenExcel.skill[skillIDTemp]
				local fuwenBeSkillID = fuwenSkillConfig.fuwenBeSkill
				setBeSkillID(obj,fuwenBeSkillID, 1)
			end
		end
	end
end

function setPetBeSkill(obj)
	--if obj
	local skillConfig = SkillExcel[1999]
	if skillConfig then
		obj.beSkillList = obj.beSkillList or {}
		obj.beSkillList[skillConfig.beSkillType] = obj.beSkillList[skillConfig.beSkillType] or {}
		obj.beSkillList[skillConfig.beSkillType][#obj.beSkillList[skillConfig.beSkillType] + 1] = {1999,skillConfig.beSkillLimit > 0 and skillConfig.beSkillLimit,skillConfig.cd[1] or 0,skillConfig.cd[2] or 0}
	end

end

--被动技能类型
BESKILL_TYPE1 = 1 --普攻附带
BESKILL_TYPE2 = 2 --被击触发
BESKILL_TYPE3 = 3 --自身生命低于参数
BESKILL_TYPE4 = 4 --自己暴击触发
BESKILL_TYPE5 = 5 --受到技能伤害
BESKILL_TYPE6 = 6 --自己攻击被格挡
BESKILL_TYPE7 = 7 --受到暴击触发
BESKILL_TYPE8 = 8 --自己格挡触发(其他单位对自己发动攻击，自己触发格挡)
BESKILL_TYPE9 = 9 --敌方英雄触发格挡
BESKILL_TYPE10 = 10 --自己受持续状态伤害
BESKILL_TYPE11 = 11 --自己受控制类状态
BESKILL_TYPE12 = 12 --场上有英雄被添加燃烧状态
BESKILL_TYPE13 = 13	-- 我方英雄施放主动技能
BESKILL_TYPE14 = 14	-- 我方英雄暴击
BESKILL_TYPE15 = 15	-- 自己死亡
BESKILL_TYPE16 = 16 --我方英雄死亡
BESKILL_TYPE17 = 17 --敌方英雄死亡
BESKILL_TYPE18 = 18 --有英雄死亡
BESKILL_TYPE19 = 19 --回合开始
BESKILL_TYPE20 = 20 --回合结束
BESKILL_TYPE23 = 23 --激活就生效(计入面板属性)
BESKILL_TYPE24 = 24 --技能包
BESKILL_TYPE25 = 25 --替换初始普攻
BESKILL_TYPE26 = 26 --激活就生效(进入战斗前算。不计入面板属性)
BESKILL_TYPE27 = 27 --魔兽回能量被动
BESKILL_TYPE28 = 28 --自身触发格挡次数
BESKILL_TYPE29 = 29 --有人受到缠绕时
BESKILL_TYPE30 = 30 --使生命低于x时
BESKILL_TYPE31 = 31 --任意英雄释放主动技能
BESKILL_TYPE32 = 32 --战斗开始时触发
BESKILL_TYPE33 = 33 --复活时触发
BESKILL_TYPE34 = 34 --使用主动技能时
BESKILL_TYPE35 = 35 --自身击杀敌人时
BESKILL_TYPE36 = 36 --攻击回合结束时触发
BESKILL_TYPE37 = 37 --物攻后触发
BESKILL_TYPE38 = 38 --法攻后触发
BESKILL_TYPE39 = 39 --攻击附带   (普攻 主动 都算) 
BESKILL_TYPE40 = 40 --攻击前根据条件判断是否触发（小战斗回合，针对每个对象单独判断）
BESKILL_TYPE41 = 41	--实际扣血前触发
BESKILL_TYPE42 = 42	--由其他英雄的buff触发
BESKILL_TYPE43 = 43 --被某个技能打死
BESKILL_TYPE44 = 44 --自己暴击+反击触发
BESKILL_TYPE45 = 45 --计算伤害时触发
BESKILL_TYPE46 = 46 --攻击附带   (普攻 主动 连击 反击都算) 
BESKILL_TYPE47 = 47 --每回合叠加属性
BESKILL_TYPE48 = 48 --死亡英雄伤害率
BESKILL_TYPE49 = 49 --加血变成减血
BESKILL_TYPE50 = 50 --每回合触发 降低对方首次攻击目标伤害率
BESKILL_TYPE51 = 51 --场上有英雄被添加诅咒状态
BESKILL_TYPE52 = 52 --场上有英雄被攻击
BESKILL_TYPE53 = 53 --场上有英雄有特定被动技能 友军普攻触发
BESKILL_TYPE54 = 54 --场上有英雄特定buff 伤害加成
BESKILL_TYPE55 = 55 --场上有英雄指定buff层数 额外伤害加成
BESKILL_TYPE56 = 56 --场上有英雄特定bufer且概率产生
BESKILL_TYPE57 = 57 --特定buff被引爆消除时 触发的效果
BESKILL_TYPE58 = 58 --指定技能暴击
BESKILL_TYPE59 = 59 --没有暴击触发一个效果
BESKILL_TYPE60 = 60 --暴击加成
BESKILL_TYPE61 = 61 --技能未击杀目标时触发
BESKILL_TYPE62 = 62 --回合开始生命值触发
BESKILL_TYPE63 = 63 --回合开始生命值触发
BESKILL_TYPE64 = 64 --冰冻buffer被击碎
BESKILL_TYPE65 = 65 --场上有英雄有特定被动技能 友军普攻暴击触发
BESKILL_TYPE66 = 66 --敌方死亡特定buff触发
BESKILL_TYPE67 = 67 --血量低于多少跟受控制触发
BESKILL_TYPE68 = 68 --血族英雄特殊处理
BESKILL_TYPE69 = 69 --血族英雄特殊处理


NO_CHECK_FANJI_LIST = {
	[BESKILL_TYPE3] = 1,
	[BESKILL_TYPE15] = 1,
	[BESKILL_TYPE16] = 1,
	[BESKILL_TYPE17] = 1,
	[BESKILL_TYPE18] = 1,
	[BESKILL_TYPE35] = 1,
	[BESKILL_TYPE36] = 1,
	[BESKILL_TYPE37] = 1,
	[BESKILL_TYPE38] = 1,
	[BESKILL_TYPE44] = 1,
	[BESKILL_TYPE46] = 1,
}

NO_CHECK_COMBO_LIST = {
	[BESKILL_TYPE1] = 1,
	[BESKILL_TYPE3] = 1,
    [BESKILL_TYPE14] = 1,
	[BESKILL_TYPE16] = 1,
	[BESKILL_TYPE17] = 1,
	[BESKILL_TYPE18] = 1,
	[BESKILL_TYPE35] = 1,
	[BESKILL_TYPE34] = 1,
	[BESKILL_TYPE25] = 1,
	[BESKILL_TYPE46] = 1,
    [BESKILL_TYPE37] = 1,
}

local function checkLimit(data)
   if data[2] and data[2] <= 0 then
	  return
   end
            
   if data[4] > 0 and data[3] > CombatImpl.round then
      return
   end
   return true
end

local function canUse(obj,data)
	if obj.isPet then
		return true
	end

	--被动技能次数限制判断
    if not checkLimit(data) then
       return
    end
	
	local skillConfig = SkillExcel[data[1]]
	if not NO_CHECK_FANJI_LIST[skillConfig.beSkillType] and CombatImpl.isFanji then
		return
	end
	
	if not NO_CHECK_COMBO_LIST[skillConfig.beSkillType] and CombatImpl.comboType > 0 then
		return
	end 

    local bNotFeng = nil

    -- boss 被动不能被封印
    -- 世界boss模式下 boss不受feng状态影响
    if CombatImpl.fightMode[1] == CombatDefine.FIGHT_MODE3 then
       if obj.type == CombatDefine.COMBAT_OBJ_TYPE3 then
          bNotFeng = true
       end
    end

    if not bNotFeng and CombatBuff.isStatus(obj,{"banBeSkill","feng"}) then
	   return
	end

    data[3] = data[3] + data[4]

	local beSkillArgs = skillConfig.beSkillArgs
	local ret = true
	--被动技能触发条件判断
	if beSkillArgs[1] then
		if beSkillArgs[1] == "hp" then
			local hp = obj.hp
			local hpMax = CombatObj.getHpMax(obj)
			if hp > hpMax * beSkillArgs[2]/10000 then
				ret = false
			end
        elseif beSkillArgs[1] == "hpOrControl" then
            ret = false
			local hp = obj.hp
			local hpMax = CombatObj.getHpMax(obj)
			if hp <= hpMax * beSkillArgs[2]/10000 then
				ret = true
			end
            if CombatBuff.isAtControl(obj) then
                ret = true
            end
		elseif beSkillArgs[1] == "fanji" then
			local r = math.random(0,10000)
			if r > beSkillArgs[2] then
				return
			end
		elseif beSkillArgs[1] == "pos" then
			local pos = obj.pos
			ret = false
			if beSkillArgs[2] then
				for i = 1,#beSkillArgs[2] do
					if pos == beSkillArgs[2][i] then
						ret  = true
						break
					end
				end
			end
		elseif beSkillArgs[1] == "noBuffCmd" then
			return not CombatBuff.isStatus(obj,beSkillArgs[2])
		elseif beSkillArgs[1] == "attackerBuffCmd"  then
			local isStatus,cmd = CombatBuff.isStatus(obj,beSkillArgs[2])
			return isStatus
		elseif  beSkillArgs[1] == "myjob"  then
			ret = false
			for k,v in ipairs(beSkillArgs[2]) do
				if v == obj.job then
					ret = true
					break
				end
			end
		elseif beSkillArgs[1] == "sideLimit"  then
			for k,v in ipairs(beSkillArgs[2]) do
				local useCnt = CombatImpl.getBeSkillSideUse(obj.pos,v[1])
				if useCnt >= v[2] then
					return
				end
			end
		elseif beSkillArgs[1] == "firstAttackOrder" then
			local side = obj.side
			if side then
				local isFirstAttack = CombatImpl.firstAttack[side]
				if beSkillArgs[2] == 1 then
					return isFirstAttack
				else
					return not isFirstAttack
				end 
			end
		end
	end
	if ret and data[2] then
		data[2] = data[2] - 1
	end
	
	if skillConfig.cmd == "hp" then
		local hp = obj.hp
		local targetMode = skillConfig.args[3]
		--英雄血量为0时不能加血
		if (hp <= 0 or CombatBuff.isStatus(obj,{"revive"})) and targetMode and targetMode[1] == 10 then
			ret = false
		end
	end
	return ret
end

--被攻击触发，需要判断攻击者状态的被动技能
local function canUseOnHit(obj,data)
	if not obj then
		return true
	end
	local skillConfig = SkillExcel[data[1]]
	local beSkillArgs = skillConfig.beSkillArgs
	local condCmd = beSkillArgs[1]
	if condCmd == "job" then
		for _,job in ipairs(beSkillArgs[2]) do
			if obj.job == job then
				return true
			end
		end
		return false	
	elseif condCmd == "buffCmd" then
		return CombatBuff.isStatus(obj,beSkillArgs[2])
	elseif condCmd == "targetHp" then
		local hp = obj.hp
		local checkHp = CombatObj.getHpMax(obj) * beSkillArgs[3] / 10000
		if beSkillArgs[2] == 1 and hp > checkHp then
			return true
		elseif  beSkillArgs[2] == 2 and hp < checkHp then
			return true
		end
	else
		return true
	end
end

local function isCanRevive(obj, attacker , data)
	local skillConfig = SkillExcel[data[1]]
	if skillConfig.cmd == "shengling" or 
       skillConfig.cmd == "revive" then
		if skillConfig.cmd == "shengling" and CombatImpl.checkSideResult(obj.pos) == true then        -- 如果是队伍最后一个死亡的不释放 圣灵复活
			return false
		end

		if CombatBuff.canRevive(obj) ~= true then
			return false
		end

		if skillConfig.cmd == "shengling" then
			if CombatBuff.isStatus(obj,{"revive"}) then
				return false
			end
		end
		if attacker and attacker.beSkillList then
			for k,skillList in pairs(attacker.beSkillList)  do
				for j,v in pairs(skillList)  do
					local config = SkillExcel[v[1]]
					
					if config and skillConfig.cmd == "shengling" and  config.cmd == "noShengling" then  --击杀方有 不准圣灵复活被动
						return false
					elseif config and config.cmd == "noRevive" then   -- 击杀方 有不准复活的  
						if #config.args > 0 then
							local canNum = config.args[1]
							local random = math.random(1, 10000)
							if random > canNum then          -- noRevive 概率不生效
								return true
							else
                                return false
                            end
						end
						return true
					end
				end
			end		
				
		end						
	end
	return true
end

function onSkillID(obj,skillID)
	local skillConfig = SkillExcel[skillID]
	if skillConfig.type == CombatDefine.SKILL_TYPE1 and obj.beSkillList[BESKILL_TYPE25] then
		for _,v in ipairs(obj.beSkillList[BESKILL_TYPE25]) do
			if canUse(obj,v) then
				local conf = SkillExcel[v[1]]
				return conf.args[1]
			end
		end	
	end
	return skillID
end

local function use(attacker,targets,data,newFrame, packBuff)
	if canUse(attacker,data) and canUseOnHit(targets and targets[1],data) then
		--print("beskill realuse:",attacker.pos,data[1])
		local skillConfig = SkillExcel[data[1]]
		local ret

        -- 二次元连击触发帧不新加帧
        if ProjectLogic.isSsecy() and skillConfig.cmd == "combo" then
           newFrame = false
        end

        if packBuff then
           CombatImpl.afterBuff()
        end

		if Skill.CMD[skillConfig.cmd] then
			if newFrame or CombatImpl.isSkillFrameEmpty() then
				--进入战斗时，未加入任何战斗帧触发被动技能时，先添加被动技能的战斗帧，可以在这个战斗帧上挂被动技能引发的额外作用
                CombatImpl.addFrame()
				CombatImpl.setSkillID(attacker, data[1]) 
			end
			ret = Skill.CMD[skillConfig.cmd](attacker,skillConfig,targets)
		end
		local isAddSkillFrame = CombatBuff.onUseBeSkill(attacker,skillConfig,targets,ret)
		--判断是否加喊话
		if skillConfig.contentType > 0 then
			if isAddSkillFrame then
				CombatImpl.addSaySkill(attacker,skillConfig)
			elseif ret or skillConfig.beSkillType == BESKILL_TYPE26 or 
                          skillConfig.beSkillType == BESKILL_TYPE23 or 
                          skillConfig.beSkillType == BESKILL_TYPE4 or 
                          skillConfig.beSkillType == BESKILL_TYPE44 then

				if CombatImpl.isSkillFrameEmpty() then
	                CombatImpl.addFrame()
					CombatImpl.setSkillID(attacker, data[1]) 
				end
				CombatImpl.setSaySkill(attacker.pos,skillConfig.id,skillConfig.contentType)
            end
		end
		CombatImpl.addBeSkillPosUse(attacker.pos,data[1])
	end
end

function onHit(attacker,targets,skillID)
	if attacker.isPet then
		return
	end
	local useSkillConfig = SkillExcel[skillID]
	if useSkillConfig.type == CombatDefine.SKILL_TYPE2 then	
		--[[for _,pos in ipairs(CombatDefine.SIDE2POS[side]) do
			local obj = CombatImpl.objList[pos]
			if obj and obj.hp > 0 and obj.beSkillList[BESKILL_TYPE13] then
				for _,v in ipairs(obj.beSkillList[BESKILL_TYPE13]) do
					use(obj,targets,v)
				end
			end
		end]]
		if attacker.beSkillList[BESKILL_TYPE34] then
			for _,v in ipairs(attacker.beSkillList[BESKILL_TYPE34]) do
				local skillConfig = SkillExcel[v[1]]
				local beSkillArgs = skillConfig.beSkillArgs
				if beSkillArgs[1] == "attackerBuffCmd" and beSkillArgs[2][1] == "shayi" then
					local shayiCnt = CombatBuff.getBuffCnt(attacker,"shayi")
					if shayiCnt > 0 then
						for i = 1,shayiCnt do
							use(attacker,targets,v,true)
							CombatImpl.instantFrameBuffer(attacker,v[1])
						end
					end
					CombatBuff.delShayi(attacker)
				elseif beSkillArgs[1] == "qusanNeedCmd" then
					local canUse = false
					for h,need in ipairs(beSkillArgs[2]) do
						if need == useSkillConfig.cmd then
							canUse = true
							break
						end
					end
					if canUse == true then
						use(attacker,targets,v,true)
					end
				elseif beSkillArgs[1] == "jiekongNeedCmd" then
					local canUse = false
					for h,need in ipairs(beSkillArgs[2]) do
						if need == useSkillConfig.cmd then
							canUse = true
							break
						end
					end
					if canUse == true then
						use(attacker,targets,v,true)
					end
				elseif beSkillArgs[1] == "skillID" then
					local canUse = false
					for _,v in ipairs(beSkillArgs[2]) do
						if skillID == v then
							canUse = true
							break
						end
					end
					if canUse == true then
						use(attacker,targets,v,true)
					end
				else
					use(attacker,targets,v)
				end
			end
		end
	end
	--非攻击性主动技能不处理被击被动技能
	if CombatObj.getValue(attacker,RoleDefine.HURT_COMBAT_NOW) <= 0 then
		if (useSkillConfig.type == CombatDefine.SKILL_TYPE1 or useSkillConfig.type == CombatDefine.SKILL_TYPE2) then
			if  attacker.beSkillList[BESKILL_TYPE39] then
				for _,v in ipairs(attacker.beSkillList[BESKILL_TYPE39]) do
					use(attacker,targets,v)
				end
			end
		end	
		return
	end

	if useSkillConfig.type == CombatDefine.SKILL_TYPE1 and attacker.beSkillList[BESKILL_TYPE1] then
		for _,v in ipairs(attacker.beSkillList[BESKILL_TYPE1]) do
			use(attacker,targets,v)
		end
	end

    if useSkillConfig.type == CombatDefine.SKILL_TYPE2 and attacker.beSkillList[BESKILL_TYPE61] then
        local canUseSkill = true
    	for index = 1, #targets do
		    local target = targets[index]
            if target.hp <=0 then
               canUseSkill = false
            end
        end

        if canUseSkill then
		   for _,v in ipairs(attacker.beSkillList[BESKILL_TYPE61]) do
                local skillConfig = SkillExcel[v[1]]
				local beSkillArgs = skillConfig.beSkillArgs
                if beSkillArgs[1] == "skillID" then
                   local canUse = false
				   for _,v in ipairs(beSkillArgs[2]) do
				   	  if skillID == v then
				   	  	 canUse = true
				   	  	 break
				   	  end
				   end
                   if canUse then
                      use(attacker,{},v)
                   end
                end
		   end
        end
	end

    

	if (useSkillConfig.type == CombatDefine.SKILL_TYPE1 or useSkillConfig.type == CombatDefine.SKILL_TYPE2 or useSkillConfig.type == CombatDefine.SKILL_TYPE3) then
		if  attacker.beSkillList[BESKILL_TYPE39] then
			for _,v in ipairs(attacker.beSkillList[BESKILL_TYPE39]) do
				use(attacker,targets,v)
			end
		end

		if attacker.beSkillList[BESKILL_TYPE46] then
			for _,v in ipairs(attacker.beSkillList[BESKILL_TYPE46]) do
				use(attacker,targets,v)
			end
		end
	end	

	local isDefendSideGedang = false
	local baojiDefender = {}
	
	for _,target in ipairs(targets) do
		if target.beSkillList[BESKILL_TYPE2] then
			for _,v in ipairs(target.beSkillList[BESKILL_TYPE2]) do
				use(target,{attacker},v)	
			end
		end

        -- 友军受击
        for _,pos in ipairs(CombatDefine.SIDE2POS[target.side]) do
           local obj = CombatImpl.objList[pos]
           if obj and obj.pos ~= target.pos and obj.beSkillList[BESKILL_TYPE52] then
			  for _,v in ipairs(obj.beSkillList[BESKILL_TYPE52]) do
			  	 use(target,{attacker},v)	
			  end
		   end
        end
         
		if useSkillConfig.type == CombatDefine.SKILL_TYPE2 and target.beSkillList[BESKILL_TYPE5] then
			for _,v in ipairs(target.beSkillList[BESKILL_TYPE5]) do
				use(target,{attacker},v)	
			end
		end
		
		if target.isGedang then
			isDefendSideGedang = true
			if target.beSkillList[BESKILL_TYPE8] then
				for _,v in ipairs(target.beSkillList[BESKILL_TYPE8]) do
					use(target,{attacker},v)	
				end
			end
			
			if attacker.beSkillList[BESKILL_TYPE6] then
				for _,v in ipairs(attacker.beSkillList[BESKILL_TYPE6]) do
					use(attacker,{target},v)	
				end
			end
			
			--目标触发格挡次数			
			if target.beSkillList[BESKILL_TYPE28] then
				for _, v in ipairs(target.beSkillList[BESKILL_TYPE28]) do
					local skillConfig = SkillExcel[v[1]]
					local beSkillArgs = skillConfig.beSkillArgs
					if beSkillArgs[1] == "gedang" then
                        target.isGedangCnt = target.isGedangCnt or 0
						local geDangLimit = beSkillArgs[2]
						if target.isGedangCnt >= geDangLimit then
						   target.isGedangCnt = 0
						   use(target,{attacker},v)
						end	
					end
				end
            end
		end
		
		local chanRao = CombatBuff.isChanRao(target)
		if  chanRao then
			if target.beSkillList[BESKILL_TYPE29]  then
				for _,v in ipairs(target.beSkillList[BESKILL_TYPE29]) do
					use(target,{attacker},v)	
				end
			end
		end
		
		if target.isBaoji then
			if target.beSkillList[BESKILL_TYPE7]  then
				for _,v in ipairs(target.beSkillList[BESKILL_TYPE7]) do
					use(target,{attacker},v)	
				end
			end
			baojiDefender[#baojiDefender+1] = target
		end
		
	end

	local side = attacker.side
	local defendSide = side == CombatDefine.ATTACK_SIDE  and CombatDefine.DEFEND_SIDE  or CombatDefine.ATTACK_SIDE 
	
    --普通技能触发
	if useSkillConfig.type == CombatDefine.SKILL_TYPE1 then
        for _,pos in ipairs(CombatDefine.SIDE2POS[side]) do
           local obj = CombatImpl.objList[pos]
           if obj and obj.hp > 0 and obj.beSkillList[BESKILL_TYPE53] then
			  for _,v in ipairs(obj.beSkillList[BESKILL_TYPE53]) do
			  	 use(attacker,targets,v)	
			  end
		   end
        end
	end
	--主动技能触发
	if useSkillConfig.type == CombatDefine.SKILL_TYPE2 then	
		for _,pos in ipairs(CombatDefine.SIDE2POS[0]) do
			local obj = CombatImpl.objList[pos]
			local objSide = pos <= 6 and CombatDefine.ATTACK_SIDE or CombatDefine.DEFEND_SIDE
			--触发自己这方的
			if side == objSide then
				if obj and obj.hp > 0 and obj.beSkillList[BESKILL_TYPE13] then
					for _,v in ipairs(obj.beSkillList[BESKILL_TYPE13]) do
						use(obj,targets,v)
					end
				end
            end 
		
			--触发任意一方
			if obj and obj.hp > 0 and obj.beSkillList[BESKILL_TYPE31] then
				for _,v in ipairs(obj.beSkillList[BESKILL_TYPE31]) do
					use(obj,targets,v)
				end
			end
		end	
	end

    if not attacker.isBaoji then
       --指定技能没有暴击
        if attacker.beSkillList[BESKILL_TYPE59] then
			for _,v in ipairs(attacker.beSkillList[BESKILL_TYPE59]) do
                local skillConfig = SkillExcel[v[1]]
				local beSkillArgs = skillConfig.beSkillArgs
                if beSkillArgs[1] == "skillID" then
                   local canUse = false
				   for _,v in ipairs(beSkillArgs[2]) do
				   	  if skillID == v then
				   	  	 canUse = true
				   	  	 break
				   	  end
				   end
                   if canUse then
                      use(attacker,{},v)
                   end
                end
			end
		end
    end

	if attacker.isBaoji then
        --普通暴击技能触发
	    if useSkillConfig.type == CombatDefine.SKILL_TYPE1 then
            for _,pos in ipairs(CombatDefine.SIDE2POS[side]) do
               local obj = CombatImpl.objList[pos]
               if obj and obj.hp > 0 and obj.beSkillList[BESKILL_TYPE65] then
	    		  for _,v in ipairs(obj.beSkillList[BESKILL_TYPE65]) do
	    		  	 use(attacker,targets,v)	
	    		  end
	    	   end
            end
	    end

		if attacker.beSkillList[BESKILL_TYPE4] then
			for _,v in ipairs(attacker.beSkillList[BESKILL_TYPE4]) do
				use(attacker,baojiDefender,v)
			end
		end

		if attacker.beSkillList[BESKILL_TYPE44] and CombatImpl.isFanji then
			for _,v in ipairs(attacker.beSkillList[BESKILL_TYPE44]) do
				use(attacker,baojiDefender,v)
			end
		end

        --指定技能暴击
        if attacker.beSkillList[BESKILL_TYPE58] then
			for _,v in ipairs(attacker.beSkillList[BESKILL_TYPE58]) do
                local skillConfig = SkillExcel[v[1]]
				local beSkillArgs = skillConfig.beSkillArgs
                if beSkillArgs[1] == "skillID" then
                   local canUse = false
				   for _,v in ipairs(beSkillArgs[2]) do
				   	  if skillID == v then
				   	  	 canUse = true
				   	  	 break
				   	  end
				   end
                   if canUse then
                      use(attacker,baojiDefender,v)
                   end
                end
			end
		end

		for _,pos in ipairs(CombatDefine.SIDE2POS[side]) do
			local obj = CombatImpl.objList[pos]
			if obj and obj.hp > 0 and obj.beSkillList[BESKILL_TYPE14] then
				for _,v in ipairs(obj.beSkillList[BESKILL_TYPE14]) do
					use(obj,targets,v)
				end
			end
		end
	end

	if isDefendSideGedang then	
		for _,pos in ipairs(CombatDefine.SIDE2POS[side]) do
			local obj = CombatImpl.objList[pos]
			if obj and obj.hp > 0 and obj.beSkillList[BESKILL_TYPE9] then
				for _,v in ipairs(obj.beSkillList[BESKILL_TYPE9]) do
					use(obj,targets,v)
				end
			end
		end
	end
end

function onHp(obj, hurt, attackPos)
	if obj.beSkillList[BESKILL_TYPE3] then
		for _,v in ipairs(obj.beSkillList[BESKILL_TYPE3]) do
			use(obj,{},v)
		end
	end

    if obj.beSkillList[BESKILL_TYPE67] then
	   for _,v in ipairs(obj.beSkillList[BESKILL_TYPE67]) do
	   	   use(obj,{},v)
	   end
	end

	local attacker = CombatImpl.objList[attackPos]
	if attacker and attacker.beSkillList[BESKILL_TYPE30] and obj.pos ~= attackPos then
		for _,v in ipairs(attacker.beSkillList[BESKILL_TYPE30]) do
			local skillConfig = SkillExcel[v[1]]
			local beSkillArgs = skillConfig.beSkillArgs
            obj.paramData.isbeSkillByHp =  obj.paramData.isbeSkillByHp or 0		
			if beSkillArgs[1] then
				if beSkillArgs[1] == "tohp" then
					local hp = obj.hp
					local hpMax = CombatObj.getHpMax(obj)
					if hp > hpMax * beSkillArgs[2]/10000 then
						obj.paramData.isbeSkillByHp = 0	--恢复到上限						
					else
                        if obj.isbeSkillByHp == 0 then						
						    obj.paramData.isbeSkillByHp = 1 --达到下限
						end
					end
				elseif beSkillArgs[1] == "hurthpper" then
					local hpMax = CombatObj.getHpMax(obj)
					local percent = -hurt/hpMax
					if percent > beSkillArgs[2]/10000  then -- 伤害超过目标生命值上限X%时			
						use(attacker,{obj},v)	
					end
				elseif beSkillArgs[1] == "speed" then
					local atkSpeed = CombatImpl.getValue(attacker,RoleDefine.SPEED)
					local defSpeed = CombatImpl.getValue(obj,RoleDefine.SPEED)
					if atkSpeed > defSpeed then
						use(attacker,{obj},v)
					end
				end
			end
			
            --使用技能			
            if obj.paramData.isbeSkillByHp == 1 then
                use(attacker,{obj},v)	
				obj.paramData.isbeSkillByHp = 2
			end
		end
	end
end


-- 选择合适的复活技能
function selectReviceSkill(obj, attacker)
   local reviveSkillList = {}
   local len = 0
   for k, v in ipairs(obj.beSkillList[BESKILL_TYPE15]) do
      local skillID = v[1]
      local isFuwenSkill = v[5]
      local skillConfig = SkillExcel[skillID]
      if skillConfig.cmd == "revive" then
         --有可使用的主动复活技能
      	 local state = isCanRevive(obj, attacker, v)
		 if state and isFuwenSkill == 0 then
            if checkLimit(v) then
                len = len + 1
                reviveSkillList[k] = 1
                break
            end
         end 
      end
   end

   -- 没有再取符文技能的复活
   if len == 0 then
      for k, v in ipairs(obj.beSkillList[BESKILL_TYPE15]) do
         local skillID = v[1]
         local isFuwenSkill = v[5]
         local skillConfig = SkillExcel[skillID]
         if skillConfig.cmd == "revive" then
         	local state = isCanRevive(obj, attacker, v)
	   	    if state and isFuwenSkill == 1 then
               --被动技能次数限制判断
               if checkLimit(v) then
                  reviveSkillList[k] = 1
                  break
               end
            end 
         end
      end
   end

   return reviveSkillList
end



function onDie(obj, hurt, attackPos)
	if obj.hp > 0 then 
		return
	end

	local side = obj.side
	local attacker = CombatImpl.objList[attackPos]
	if obj.attackSkillID then
		if attacker and attacker.beSkillList[BESKILL_TYPE43] then
			for _,v in ipairs(attacker.beSkillList[BESKILL_TYPE43]) do
				local skillConfig = SkillExcel[v[1]]
				for _,v1 in ipairs(skillConfig.beSkillArgs) do
					if  v1 == obj.attackSkillID then
						use(attacker,{obj},v)
						break
					end					
				end

			end
		end
	end
	

	local defendSide = side == CombatDefine.ATTACK_SIDE and CombatDefine.DEFEND_SIDE or CombatDefine.ATTACK_SIDE
	for _,posData in ipairs(posSortBySpeed[side]) do
		local pos = posData[1]
		local target = CombatImpl.objList[pos]
		if target and target.hp > 0 then
			if target.beSkillList[BESKILL_TYPE16] then
				for _,v in ipairs(target.beSkillList[BESKILL_TYPE16]) do
					use(target,{obj},v)
				end
			end
			if target.beSkillList[BESKILL_TYPE18] then
				for _,v in ipairs(target.beSkillList[BESKILL_TYPE18]) do
					use(target,{obj},v)
				end
			end
		end
	end

	for _,posData in ipairs(posSortBySpeed[defendSide]) do
		local pos = posData[1]	
		local target = CombatImpl.objList[pos]
		if target and target.hp > 0 then
			if target.beSkillList[BESKILL_TYPE17] then
				for _,v in ipairs(target.beSkillList[BESKILL_TYPE17]) do
					use(target,{obj},v)
				end
			end
			if target.beSkillList[BESKILL_TYPE18] then
				for _,v in ipairs(target.beSkillList[BESKILL_TYPE18]) do
					use(target,{obj},v)
				end
			end
		end
	end	
	
	if attacker and attacker.beSkillList and attacker.beSkillList[BESKILL_TYPE30] and obj.pos ~= attackPos then
		for _,v in ipairs(attacker.beSkillList[BESKILL_TYPE30]) do
			if obj.isbeSkillByHp == nil or 
			   obj.isbeSkillByHp == 0 then
				use(attacker,{},v)
			end
		end
	end

	if attacker and attacker.beSkillList and attacker.beSkillList[BESKILL_TYPE35] then
		for _,v in ipairs(attacker.beSkillList[BESKILL_TYPE35]) do
			use(attacker,{obj},v)
		end
	end

	if obj.beSkillList[BESKILL_TYPE15] then
        -- 选择其中一个使用
        local reviceSkillList = selectReviceSkill(obj, attacker)
		for k, v in ipairs(obj.beSkillList[BESKILL_TYPE15]) do
            local skillID = v[1]
            local skillConfig = SkillExcel[skillID]
            if skillConfig.cmd == "revive" then
                if reviceSkillList[k] then
                   use(obj,{},v)
                   break
                end
            else
                use(obj,{},v)
            end		
		end	
	end
	
    	   
    for _,pos in ipairs(CombatDefine.SIDE2POS[defendSide]) do
	   local defendobj = CombatImpl.objList[pos]
       if defendobj and defendobj.hp > 0 and defendobj.beSkillList[BESKILL_TYPE66] then
          for _,v in ipairs(defendobj.beSkillList[BESKILL_TYPE66]) do
             local skillConfig = SkillExcel[v[1]]
             if skillConfig and skillConfig.otherArgs.tdBuffCmd then
                if obj.bufferCmd[skillConfig.otherArgs.tdBuffCmd] then
                   use(defendobj,{},v)
                end
             end
          end
       end
    end
end

function onAddBuffer(obj,buffer)
	local conf = BufferExcel[buffer.id]
	local attacker = CombatImpl.objList[buffer.attackPos]
	local cmd = conf.cmd
	if cmd == "hurt" or cmd == "liuxue" or cmd == "zhongdu" or cmd == "ranshao" then
		if obj.beSkillList[BESKILL_TYPE10] then
			for _,v in ipairs(obj.beSkillList[BESKILL_TYPE10]) do
				use(obj,{attacker},v)
			end
		end	
	end

    if cmd == "bingdong" or cmd == "jiansu" then
        for _,pos in ipairs(CombatDefine.SIDE2POS[attacker.side]) do
			local target = CombatImpl.objList[pos]
			if target and target.hp > 0 and target.beSkillList[BESKILL_TYPE63] then
				for _,v in ipairs(target.beSkillList[BESKILL_TYPE63]) do
				   use(target,{obj},v)
				end		
			end	
		end
	end
	
	if CombatBuff.isControlCmd(cmd) then
		if obj.beSkillList[BESKILL_TYPE11] then
			for _,v in ipairs(obj.beSkillList[BESKILL_TYPE11]) do
				use(obj,{attacker},v)
			end
		end
        
        if obj.beSkillList[BESKILL_TYPE67] then
			for _,v in ipairs(obj.beSkillList[BESKILL_TYPE67]) do
				use(obj,{attacker},v, true, true)
			end
		end		
	end
	
	if cmd == "ranshao" then
		for pos = 1,CombatDefine.COMBAT_HERO_ALL_CNT do
			local target = CombatImpl.objList[pos]
			if target and target.hp > 0 and target.beSkillList[BESKILL_TYPE12] then
				for _,v in ipairs(target.beSkillList[BESKILL_TYPE12]) do
					local beSkillUseCnt = CombatImpl.getBeSkillUse(target,obj.pos,v[1])--特殊条件特殊处理:同一英雄在同一时间被施加多次燃烧状态时只触发一次
					if beSkillUseCnt == 0 then
						use(target,{obj},v)
						CombatImpl.addBeSkillUse(target,obj.pos,v[1])
					end
				end		
			end	
		end
	end

    if cmd == "zuzhou" then
        for _,pos in ipairs(CombatDefine.SIDE2POS[attacker.side]) do
			local target = CombatImpl.objList[pos]
			if target and target.hp > 0 and target.beSkillList[BESKILL_TYPE51] then
				for _,v in ipairs(target.beSkillList[BESKILL_TYPE51]) do
				   use(target,{obj},v)
				end		
			end	
		end
	end

	if cmd == "revive" then
		if obj.beSkillList[BESKILL_TYPE33] then
			for _,v in ipairs(obj.beSkillList[BESKILL_TYPE33]) do
				use(obj,{attacker},v)
			end
		end	
	end	

end


function onDelBingDongBuffer(obj, buffer)
	local conf = BufferExcel[buffer.id]
	local attacker = CombatImpl.objList[buffer.attackPos]
    if conf.cmd == "bingdong" then
        for _, pos in ipairs(CombatDefine.SIDE2POS[attacker.side]) do
			local target = CombatImpl.objList[pos]
			if target and target.hp > 0 and target.beSkillList[BESKILL_TYPE64] then
				for _,v in ipairs(target.beSkillList[BESKILL_TYPE64]) do
				   use(target,{obj},v)
				end		
			end	
		end
	end
end

--援军上阵触发
function onBackupBegin(obj)
    if not obj then return end
    if CombatDefine.isBackup(obj) ~= CombatDefine.BACKUP_TYPE2 then return end

    -- 备份的被动技能上阵
    if obj.backUpSkillList then
       obj.beSkillList = obj.backUpSkillList
       obj.backUpSkillList = nil
    end

    -- 出站计算属性
   	if obj.beSkillList[BESKILL_TYPE26] then
	    for _,v in ipairs(obj.beSkillList[BESKILL_TYPE26]) do
	    	 if canUse(obj,v) then
	    	 	local skillConfig = SkillExcel[v[1]]
	    	 	if skillConfig.cmd == "attr" then
	    	 		for _,v1 in ipairs(skillConfig.args) do
	    	 			obj.sysAttr[v1[1]] = obj.sysAttr[v1[1]] +v1[2]
	    	 		end
	    	 		obj.isSysAttrChange = true
	    	 	elseif skillConfig.cmd == "normalTarget" then
	    	 		use(obj,{},v)
	    	 	end
	    	 end
	    end
	end
    table.sort(posSortBySpeed[CombatDefine.ATTACK_SIDE],posSpeedCmp)
	table.sort(posSortBySpeed[CombatDefine.DEFEND_SIDE],posSpeedCmp)

    --援军出战触发
	if obj and obj.beSkillList[BESKILL_TYPE32] then
		for k,v in ipairs(obj.beSkillList[BESKILL_TYPE32]) do
			use(obj,{},v)
		end
	end

    --上场技能
    CombatDefine.BACKUP_BUFF_SKILL[2] = 1
    use(obj,{}, CombatDefine.BACKUP_BUFF_SKILL)	
end


function onRoundBegin(round)
	for pos = 1,CombatDefine.COMBAT_HERO_ALL_CNT do
		local target = CombatImpl.objList[pos]
		if target and target.hp > 0 and target.beSkillList[BESKILL_TYPE19] then
			for _,v in ipairs(target.beSkillList[BESKILL_TYPE19]) do
				use(target,{},v)
			end
		end
        
        -- 生命值触发
        if target and target.hp > 0 and target.beSkillList[BESKILL_TYPE62] then
			for _,v in ipairs(target.beSkillList[BESKILL_TYPE62]) do
            	local skillConfig = SkillExcel[v[1]]
			    local beSkillArgs = skillConfig.beSkillArgs

                if beSkillArgs[1] == 1 then
                   local hpMax = CombatObj.getHpMax(target)
                   local percent = target.hp/hpMax
                   if percent > beSkillArgs[2]/10000 then
                      use(target,{},v)
                   end
                end

                if beSkillArgs[1] == 2 then
                   local hpMax = CombatObj.getHpMax(target)
                   local percent = target.hp/hpMax
                   if percent < beSkillArgs[2]/10000 then
                      use(target,{},v)
                   end
                end
			end
		end				
	end

 
    for pos = 1,CombatDefine.COMBAT_HERO_ALL_CNT do
		local target = CombatImpl.objList[pos]
		if target and target.hp > 0 and target.beSkillList[BESKILL_TYPE47] then
			for _,v in ipairs(target.beSkillList[BESKILL_TYPE47]) do
                local skillConfig = SkillExcel[v[1]]
				if skillConfig.cmd == "attr" then
					for _,v1 in ipairs(skillConfig.args) do
						target.sysAttr[v1[1]] = target.sysAttr[v1[1]] +v1[2]
					end
                    CombatObj.calcAttr(target)
	                CombatImpl.addFrame()
					CombatImpl.setSkillID(target, v[1])
                    CombatImpl.setSaySkill(target.pos,skillConfig.id,skillConfig.contentType)
                end
			end
		end		
	end
    
	if round and round == 1 then
		for pos = 1,CombatDefine.COMBAT_HERO_ALL_CNT do
			local obj = CombatImpl.objList[pos]
			if obj and obj.beSkillList[BESKILL_TYPE32] then
				for k,v in ipairs(obj.beSkillList[BESKILL_TYPE32]) do
					use(obj,{},v)
				end
			end
		end
	end
end

function beforeFanji(obj)
	if not obj or obj.isPet then
		return 
	end

	if obj and obj.hurtType == CombatDefine.PHY_HURT_TYPE then
		if obj and obj.beSkillList and obj.beSkillList[BESKILL_TYPE37] then
			for _,v in ipairs(obj.beSkillList[BESKILL_TYPE37]) do
				use(obj,{},v)
			end
		end
	else
		if obj and obj.beSkillList and obj.beSkillList[BESKILL_TYPE38] then
			for _,v in ipairs(obj.beSkillList[BESKILL_TYPE38]) do
				use(obj,{},v)
			end
		end
	end
end

function onRoundEnd(isRoundEnd)
	for pos = 1,CombatDefine.COMBAT_HERO_ALL_CNT do
		local obj = CombatImpl.objList[pos]
		if obj and obj.beSkillList[BESKILL_TYPE36] then
			if pos == 1 then
			end
			for _,v in ipairs(obj.beSkillList[BESKILL_TYPE36]) do
				use(obj,{},v)
			end
		end
	end	

	if isRoundEnd then
		for pos = 1,CombatDefine.COMBAT_HERO_ALL_CNT do
			local target = CombatImpl.objList[pos]
			if target and target.hp > 0 and target.beSkillList[BESKILL_TYPE20] then
				for _,v in ipairs(target.beSkillList[BESKILL_TYPE20]) do
					use(target,{},v)
				end
			end
		end
	end
end

function posSpeedCmp(a,b)
	if a[2] > b[2] then
		return true
	elseif a[2] < b[2] then
		return false
	else
		return a[1] < b[1]
	end
end

function onFightBegin()
	posSortBySpeed[CombatDefine.ATTACK_SIDE] = {}
	posSortBySpeed[CombatDefine.DEFEND_SIDE] = {}
	for pos = 1,CombatDefine.COMBAT_HERO_ALL_CNT do
		local obj = CombatImpl.objList[pos]
		if obj then
			if obj.beSkillList[BESKILL_TYPE26] then
				for _,v in ipairs(obj.beSkillList[BESKILL_TYPE26]) do
					if canUse(obj,v) then
						local skillConfig = SkillExcel[v[1]]
						if skillConfig.cmd == "attr" then
							for _,v1 in ipairs(skillConfig.args) do
								obj.sysAttr[v1[1]] = obj.sysAttr[v1[1]] +v1[2]
							end
							obj.isSysAttrChange = true
						elseif skillConfig.cmd == "normalTarget" then
							use(obj,{},v)
						end
					end
				end
			end
			if pos <= CombatDefine.COMBAT_HERO_CNT then
				posSortBySpeed[CombatDefine.ATTACK_SIDE][#posSortBySpeed[CombatDefine.ATTACK_SIDE]+1] = {pos,obj.attr and obj.attr[RoleDefine.SPEED] or 0}
			elseif pos <= CombatDefine.COMBAT_HERO_CNT * 2 then
				posSortBySpeed[CombatDefine.DEFEND_SIDE][#posSortBySpeed[CombatDefine.DEFEND_SIDE]+1] = {pos,obj.attr and obj.attr[RoleDefine.SPEED] or 0}
			end
		end
	end
	table.sort(posSortBySpeed[CombatDefine.ATTACK_SIDE],posSpeedCmp)
	table.sort(posSortBySpeed[CombatDefine.DEFEND_SIDE],posSpeedCmp)
end

function onCombatPetBe(round)
	if round <= 1 then
		return
	end
	for side = 1,CombatDefine.DEFEND_SIDE do
		local pet = CombatImpl.petList[side] 
		if pet and pet.petID and pet.beSkillList[BESKILL_TYPE27] then
			for _,v in ipairs(pet.beSkillList[BESKILL_TYPE27]) do
				local skillConfig = SkillExcel[v[1]]
				if skillConfig.cmd == "mp" then
					use(pet,{},v)
				end
			end
		end
	end
end

-- 计算被动技能属性
local beSkillIDList = {}
function doCalcHero(obj, attrs, skinSkillID, heroConf)
	for i = 1, #beSkillIDList do
		beSkillIDList[i] = nil
	end		
	heroConf = heroConf or HeroDefine.getAttrConfig(obj.id, obj.star)
	local beSkillIDs = heroConf.beSkillList
	local skinSkillConf = skinSkillID and SkinSkillExcel[skinSkillID] 
	if skinSkillConf then
		beSkillIDs = skinSkillConf.beSkillList
	end
	for _, skillID in ipairs(beSkillIDs) do
		local skillConfig = SkillExcel[skillID]
		if skillConfig.beSkillType == BESKILL_TYPE24 and 
			((not obj) or (obj.quality >= skillConfig.need)) then
			for _,v in ipairs(skillConfig.args) do
				beSkillIDList[#beSkillIDList + 1] = v
			end
		else
			beSkillIDList[#beSkillIDList + 1] = skillID
		end
	end
	for _, skillID in ipairs(beSkillIDList) do
		local skillConfig = SkillExcel[skillID]
		if skillConfig and skillConfig.cmd == "attr" and
			skillConfig.beSkillType == BESKILL_TYPE23 and 
			((not obj) or (obj.quality >= skillConfig.need)) then
			for _, v in ipairs(skillConfig.args) do
				RoleAttr.updateValue(v[1], v[2], attrs)
			end
		end
	end	
end

function beforeHit(attacker,defender)
	attacker.beforeHitCheck = false
	if attacker.beSkillList[BESKILL_TYPE40] then
		local hpMax = CombatObj.getHpMax(defender)
		local hp = defender.hp
		local defendHpRate = hp*10000/hpMax
		for _,v in ipairs(attacker.beSkillList[BESKILL_TYPE40]) do
			local skillConfig = SkillExcel[v[1]]
			if skillConfig.cmd == "lowerLimit" then
				local args = skillConfig.args
				if defendHpRate < args[1] then
					attacker.beforeHitCheck = true
					attacker.beskill40 = attacker.beskill40 or {}
					attacker.beskill40[defender.pos] = 1
				end
			end
		end	
	end
end

function afterHit(attacker,target, isEnd)
	if attacker.beforeHitCheck then
		for _,v in ipairs(attacker.beSkillList[BESKILL_TYPE40]) do
			use(attacker,{target},v)
		end	
	end
	beforeFanji(attacker, isEnd)
end


function getOutAtkRate(attacker, defender)
    local outAtkRate = 0
    -- 首次攻击
    local isFirstAttack = CombatImpl.firstAttack[attacker.side]
    if isFirstAttack then
       for _,pos in ipairs(CombatDefine.SIDE2POS[defender.side]) do
          local objM = CombatImpl.objList[pos]
          if objM and objM.beSkillList[BESKILL_TYPE50] then
             for _,v in ipairs(objM.beSkillList[BESKILL_TYPE50]) do
                local skillConfig = SkillExcel[v[1]]
                if skillConfig and skillConfig.otherArgs.outAtkRate then
                   if skillConfig.otherArgs.outAtkRate >= outAtkRate then
                      outAtkRate = skillConfig.otherArgs.outAtkRate
                   end
                end
             end
          end
       end 
    end
    return outAtkRate
end


function getExtraHurtRate(attacker,defender,skillConfig)
	if not attacker or attacker.isPet then
		return 0
	end

	local zhanyiRet = 0	
	local bodongHurtRet = 0
	local zhenduiHurtRate = 0
	local zhenduiLen = 0
	local zhenduiList = {}
	local bezhenduiList = {}
	local hpHurtRate = 0
	local extraSpeedHurt = 0
	local firstHurt = 0
    local sexHurt = 0
    local campHurt = 0
    local reJobHurt = 0
	for k,skillList in pairs(attacker.beSkillList)  do
		for j,v in pairs(skillList)  do
			local config = SkillExcel[v[1]]
			if config.cmd == "zhanyi" then  --战意 伤害加成
				local nowHp = attacker.hp
				local hpMax = CombatObj.getHpMax(attacker) 
				local percent = math.floor( (hpMax - nowHp )/hpMax * 100)
				if percent >= 1 then				
					zhanyiRet = zhanyiRet + (percent * config.args[2])
				end	
			elseif config.cmd == "fuwenAKH" then
				for i = 1,#config.otherArgs do
					zhenduiLen = zhenduiLen + 1
					zhenduiList[config.otherArgs[i][1]] = zhenduiList[config.otherArgs[i][1]] or 0
					zhenduiList[config.otherArgs[i][1]] = zhenduiList[config.otherArgs[i][1]] + config.otherArgs[i][2]
				end			
			elseif config.cmd == "hurtBodong" then
				if canUse(attacker, v) then
					local minRet = config.args[1]
					local maxRet = config.args[2]
					local randomRet = math.random(minRet,  maxRet)
					bodongHurtRet = bodongHurtRet + randomRet - 10000					
				end
			elseif config.cmd == "hpExtraHurt" then
				local hp = defender.hp
				local beSkillArgs = config.beSkillArgs
				local checkHp = CombatObj.getHpMax(defender) * beSkillArgs[3] / 10000
				if beSkillArgs[2] == 1 and hp > checkHp then
					hpHurtRate = beSkillArgs[4]
				elseif  beSkillArgs[2] == 2 and hp < checkHp then
					hpHurtRate = beSkillArgs[4]
				end
			elseif config.cmd == "extraSpeedHurt" then
				extraSpeedHurt = config.args[1]
			elseif config.cmd == "firstAttackHurt" then
				if canUse(attacker, v) then
					firstHurt = config.args[1]
				end
			end
		end		
	end

	for k,skillList in pairs(defender.beSkillList)  do
		for j,v in pairs(skillList)  do
			local conf = SkillExcel[v[1]]
			if conf then
                if conf.cmd == "befuwenAKH" then
				   for i = 1,#conf.otherArgs do
				   	   zhenduiLen = zhenduiLen + 1
				   	   bezhenduiList[conf.otherArgs[i][1]] = bezhenduiList[conf.otherArgs[i][1]] or 0
				   	   bezhenduiList[conf.otherArgs[i][1]] = bezhenduiList[conf.otherArgs[i][1]] + conf.otherArgs[i][2]
				   end
                -- 受性别增伤
                elseif conf.cmd == "sexHurt" then
                   if attacker.sex == conf.args[1] then
                      sexHurt = sexHurt + conf.args[2]
                   end
                -- 受阵营增伤
                elseif conf.cmd == "campHurt" then
                   if attacker.camp == conf.args[1] then
                      campHurt = campHurt + conf.args[2]
                   end
                -- 受职业增伤
                elseif conf.cmd == "reJobHurt" then
                   if attacker.job == conf.args[1] then
                      reJobHurt = reJobHurt + conf.args[2]
                   end
                end
			end
		end
	end


	if zhenduiLen > 0 then
        local bAdd = 0
		for k,skillList in pairs(defender.beSkillList) do
			for j,v in pairs(skillList)  do
				local conf = SkillExcel[v[1]]
                local isFuWenSkill = v[5]	
				if conf.cmd and conf.cmd ~= "" and zhenduiList[conf.cmd] then
                    -- 符文技能只生效一个
                    if isFuWenSkill == 1 then
                       if bAdd == 0 then
                          zhenduiHurtRate = zhenduiHurtRate + zhenduiList[conf.cmd]
                          bAdd = 1
                       end
                    else
                       zhenduiHurtRate = zhenduiHurtRate + zhenduiList[conf.cmd]
                    end
				end
			end
		end

        bAdd = 0
		for k,skillList in pairs(attacker.beSkillList)  do
			for j,v in pairs(skillList)  do
				local conf = SkillExcel[v[1]]
                local isFuWenSkill = v[5]	
				if conf.cmd and conf.cmd ~= "" and bezhenduiList[conf.cmd] then
                    if isFuWenSkill == 1 then
                       if bAdd == 0 then
					      zhenduiHurtRate = zhenduiHurtRate + bezhenduiList[conf.cmd]
                          bAdd = 1
                       end	
                    else
                       zhenduiHurtRate = zhenduiHurtRate + bezhenduiList[conf.cmd]
                    end
				end	
			end	
		end
	end
    
    --特殊buff 对血族的加成
    local campHurtRate = 0
    local campRate = nil
    local buffCmd = nil
    local camp = nil
    for _,pos in ipairs(CombatDefine.SIDE2POS[attacker.side]) do
       obj = CombatImpl.objList[pos]
       if obj and obj.hp > 0 then
          if obj.beSkillList[BESKILL_TYPE68] then
             local beSkillList = obj.beSkillList[BESKILL_TYPE68][1]
             local skillConfig = SkillExcel[beSkillList[1]]
             if skillConfig then
                camp = skillConfig.otherArgs.calcCamp[1]
                buffCmd = skillConfig.otherArgs.calcCamp[2]
                campRate = skillConfig.otherArgs.calcCamp[3]
                break
             end
	      end
       end
    end
    
    if buffCmd and campRate and attacker.camp == camp then
       local campHurtCnt = 0
       for _,pos in ipairs(CombatDefine.SIDE2POS[defender.side]) do
          obj = CombatImpl.objList[pos]
          if obj and obj.hp > 0 then
	    	   local isStatus1, cmd1, ind1 = CombatBuff.isStatus(obj, buffCmd)
              if isStatus1 then
                 campHurtCnt = campHurtCnt + 1
              end
	       end
       end
       if campHurtCnt > 0 then
          campHurtRate = campHurtCnt * campRate   
       end
    end

	local extraHurtRate = zhanyiRet + zhenduiHurtRate + bodongHurtRet + hpHurtRate + extraSpeedHurt + firstHurt + sexHurt + campHurt + reJobHurt + campHurtRate
	if attacker.beSkillList[BESKILL_TYPE40] and attacker.beforeHitCheck then
		for _,v in ipairs(attacker.beSkillList[BESKILL_TYPE40]) do
			local skillConfig = SkillExcel[v[1]]
			if skillConfig.cmd == "extraHurt" then
				local args = skillConfig.args
				extraHurtRate = extraHurtRate + args[1]
			end
		end	
	end

	return extraHurtRate
end

--
function getExtraHurt(attacker,target)
    local extraHurt = 0
    if attacker.beSkillList[BESKILL_TYPE55] then
       local beSkillList = attacker.beSkillList[BESKILL_TYPE55][1]
       local skillConfig = SkillExcel[beSkillList[1]]
       local buffCmd = target.bufferCmd[skillConfig.otherArgs.buffCmdHurtRate[1]]
       local rate = skillConfig.otherArgs.buffCmdHurtRate[2] / 10000
       if buffCmd then
          local buffCnt = #buffCmd
          local buffIndex = buffCmd[buffCnt]
          local buffer = target.buffer[buffIndex]
          local conf = BufferExcel[buffer.id]
          local hurt = buffer.arg[1]
          extraHurt = extraHurt + hurt * rate * buffCnt
       end
    end
    return extraHurt
end


--
function getRandomBuffCmd(attacker, target, cmd)
    local side = attacker.side
    local mathRandom = math.random
    for _,pos in ipairs(CombatDefine.SIDE2POS[side]) do
	   local obj = CombatImpl.objList[pos]
       if obj and obj.hp > 0 and obj.beSkillList[BESKILL_TYPE56] then
          local beSkillList = obj.beSkillList[BESKILL_TYPE56][1]
          local skillConfig = SkillExcel[beSkillList[1]]
          local buffCmdRand = skillConfig.otherArgs.buffCmdRand
          if buffCmdRand and cmd == buffCmdRand[1] then 
             local random = mathRandom(1, 10000)
             if random <= buffCmdRand[2] then
                return true
             end
          end
       end
    end
end

function getHurtPercentRate(attacker, defender, hurt)
	local rate = 0

	for k,skillList in pairs(defender.beSkillList)  do
		for j,v in pairs(skillList)  do
			local conf = SkillExcel[v[1]]
	
			if conf and conf.cmd == "behurtRet" then
				local canUse = true 
				if conf.beSkillArgs and #conf.beSkillArgs > 0 then
					if  conf.beSkillArgs[1] ==  "hurthpper" then
						local hpMax = CombatObj.getHpMax(defender)
						local percent = hurt/hpMax
						if percent < conf.beSkillArgs[2]/10000  then -- 伤害超过目标生命值上限X%时			
							canUse = false
						end	
					end
				end
				if canUse == true then
					local random = math.random(1, 10000)
					if random <= conf.args[2] then
						rate = rate + conf.args[1] 						
					end					
				end					
			end

		end
	end
	
	return rate
end

function getBufferExtraHurt(attacker,bufferID,cmd)
	local rate = 0

	for _,skillList in pairs(attacker.beSkillList)  do
		for k,v in pairs(skillList)  do
			local conf = SkillExcel[v[1]]
			local otherArgs = conf.otherArgs
			if bufferID and otherArgs.bufferExtraHurt then
				for k1,v1 in ipairs(otherArgs.bufferExtraHurt[1]) do
					if bufferID == v1 then
						rate = rate + otherArgs.bufferExtraHurt[2]
					end
				end
			end
			if cmd and otherArgs.bufferCmdExtraHurt then
				for k1,v1 in ipairs(otherArgs.bufferCmdExtraHurt[1]) do
					if cmd == v1 then
						rate = rate + otherArgs.bufferCmdExtraHurt[2]
					end
				end
			end
		end
	end
	return rate
end

function getExtraHurtRateEx(attacker, defender)
	local rate = 0
    -- 死亡数伤害率
    if attacker.beSkillList[BESKILL_TYPE48] then
       for _,v in ipairs(attacker.beSkillList[BESKILL_TYPE48]) do
          local skillConfig = SkillExcel[v[1]]
          if skillConfig and skillConfig.otherArgs.deathHurt then
             local side = defender.side
             for _,pos in ipairs(CombatDefine.SIDE2POS[side]) do
		         local obj = CombatImpl.objList[pos]
		         if obj and obj.hp <=0 then
                    if rate == 0 then
                       rate = skillConfig.otherArgs.deathHurt
                    else
                       rate = rate + 10000
                    end
		         end
	         end
          end
       end
    end

	return rate
end

--
function getBuffChixuHurt(attacker, cmd)
   if not attacker then return 0 end
   local hurt = 0
   local side = attacker.side
   for _,pos in ipairs(CombatDefine.SIDE2POS[side]) do
	   local objM = CombatImpl.objList[pos]
       if objM and objM.hp > 0 and objM.beSkillList[BESKILL_TYPE54] then
          for _,v in ipairs(objM.beSkillList[BESKILL_TYPE54]) do
             local skillConfig = SkillExcel[v[1]]
             if skillConfig and skillConfig.otherArgs[cmd] then
                hurt = hurt + skillConfig.otherArgs[cmd]
             end
          end
       end
   end
   return hurt / 10000
end

--
function handlerBuff(attacker, target, arg, cmd)
   if not attacker then return end

   for _,pos in ipairs(CombatDefine.SIDE2POS[attacker.side]) do
	   local obj = CombatImpl.objList[pos]
       if obj and obj.hp > 0 and obj.beSkillList[BESKILL_TYPE57] then
           for _,v in ipairs(obj.beSkillList[BESKILL_TYPE57]) do

              attacker.args = arg
              use(attacker, {target}, v, false)
           end
       end
   end
end


function beforeMinusHp(obj, value)
    if value <= 0 then return end
    --敌对阵营 有对象有 49号被动技能 加血变成减血
    local side = obj.side == CombatDefine.ATTACK_SIDE and CombatDefine.DEFEND_SIDE or CombatDefine.ATTACK_SIDE
    for _,pos in ipairs(CombatDefine.SIDE2POS[side]) do
	    local objM = CombatImpl.objList[pos]
        if objM and objM.hp > 0 and objM.beSkillList[BESKILL_TYPE49] then
	       value = value * (-1)
           break
        end
    end

    return value
end

function beforeUpdateHp(obj,sum)
	if sum <= 0 and obj.beSkillList[BESKILL_TYPE41] then
		for _,v in ipairs(obj.beSkillList[BESKILL_TYPE41]) do
			local skillConfig = SkillExcel[v[1]]
			if skillConfig.cmd == "miansi" and canUse(obj,v) then
				use(obj,{},v)
				return true
			end
		end	
	end
end

function onCheckBuffer(obj,attacker,value)
	local ret = value
	if obj.beSkillList[BESKILL_TYPE42] then
		for _,v in ipairs(obj.beSkillList[BESKILL_TYPE42]) do
			local skillConfig = SkillExcel[v[1]]
			if canUse(obj,v) then
                local newFrame = true
				if skillConfig.cmd == "baohu" then
					obj.baohuHurt = value * skillConfig.args[1] / 10000
					if obj.hp < -obj.baohuHurt then
						obj.baohuHurt = -obj.hp
					end
					ret = value - obj.baohuHurt
					newFrame = false
                end

                use(obj,{attacker},v, newFrame)
				break
			end
		end	
	end
	return ret
end

function getStatusBaoji(attacker, defender)
    local baoji = 0
    local firstBaoji = 0
    for k, skillList in pairs(attacker.beSkillList) do
        for j, v in pairs(skillList) do
            local config = SkillExcel[v[1]]
            if config.cmd == "firstAttackHurt" then
                if canUse(attacker, v) then
                    firstBaoji = config.args[2] or 0
                end
            end
        end
    end

    if attacker.beSkillList[BESKILL_TYPE60] then
        for _, v in ipairs(attacker.beSkillList[BESKILL_TYPE60]) do
            local skillConfig = SkillExcel[v[1]]
            if skillConfig and skillConfig.otherArgs.addStatusBaoji then
                local status = CombatBuff.getStatus(defender)
                local isStatus = false
                for k, v in ipairs(skillConfig.otherArgs.addStatusBaoji[1]) do
                    if status[v] then
                        isStatus = true
                        break
                    end
                end
                if isStatus then
                    baoji = baoji + skillConfig.otherArgs.addStatusBaoji[2]
                end
            end
        end
    end

    if attacker.beSkillList[BESKILL_TYPE69] and attacker.notBaoJi then
        for _, v in ipairs(attacker.beSkillList[BESKILL_TYPE69]) do
            local skillConfig = SkillExcel[v[1]]
            if skillConfig and skillConfig.otherArgs.addNotBaoji then
                baoji = baoji + skillConfig.otherArgs.addNotBaoji
            end
        end
    end

    return baoji + firstBaoji
end

function getStatusPojia(attacker, defender)
   local poJia = 0
      -- 对指定状态破甲加成  走的技能状态属性
   if attacker.beSkillList[BESKILL_TYPE60] then
      for _, v in ipairs(attacker.beSkillList[BESKILL_TYPE60]) do
          local skillConfig = SkillExcel[v[1]]
          if skillConfig.otherArgs.addStatusPoJia then
              local status = CombatBuff.getStatus(defender)
              local isStatus = false
              for k, v in ipairs(skillConfig.otherArgs.addStatusPoJia[1]) do
                  if status[v] then
                      isStatus = true
                      break
                  end
              end
              if isStatus then
                  poJia = poJia + skillConfig.otherArgs.addStatusPoJia[2]
              end
          end
      end
   end
   return poJia
end